package com.ray.util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import com.alibaba.fastjson.JSONObject;
import com.ray.util.weixin.WeixinTemplateSender;

public class UnicodeNativeUtils {
	public static void main(String[] args) throws UnsupportedEncodingException {
		System.out.println(ChangeGB2NCR("马屿镇轩昊数控公司"));
		System.out.println(ChangeGB2NCR("沈世冠"));
	
		JSONObject json = new JSONObject();
		json.put("sfzh", "330381199208224153");
        json.put("xm", URLEncoder.encode("沈世冠", "UTF-8"));
        json.put("sjh", "15257086961");
        json.put("cph", "");
        json.put("kkid", "2238");
        json.put("kkmc", URLEncoder.encode("马屿镇轩昊数控公司", "UTF-8"));
        json.put("cr", 0);
		System.out.println(WeixinTemplateSender.httpRequest(
				"http://dx.raga.gov.cn:8921/mst/api/check.jsp?t=1583368597886"
				+ "&sfzh=330381199208224153"
				+ "&xm="+URLEncoder.encode("沈世冠", "UTF-8")//+UnicodeNativeUtils.ChangeGB2NCR("沈世冠")
				+ "&sjh=15257086961"
				+ "&cph="
				+ "&kkid=2238"
				+ "&kkmc="+URLEncoder.encode("马屿镇轩昊数控公司", "UTF-8")
				+ "&cr=0",
				"GET", null));
		
		/*System.out.println(WeixinTemplateSender.httpRequest(
				"http://dx.raga.gov.cn:8921/mst/api/check.jsp?t=1583368597886",
				"POST", json.toString()));*/
	}

	public static String decode(String str) {
		StringBuffer sb = new StringBuffer();
		int i1=0;
		int i2=0;
		while(i2<str.length()) {
			i1 = str.indexOf("&#",i2);
			if (i1 == -1 ) {
				sb.append(str.substring(i2));
				break ;
			}
			sb.append(str.substring(i2, i1));
			i2 = str.indexOf(";", i1);
			if (i2 == -1 ) {
				sb.append(str.substring(i1));
				break ;
			}
			String tok = str.substring(i1+2, i2);
			try {
				int radix = 10 ;
				if (tok.charAt(0) == 'x' || tok.charAt(0) == 'X') {
					radix = 16 ;
					tok = tok.substring(1);
				}
					sb.append((char) Integer.parseInt(tok, radix));
			} catch (NumberFormatException exp) {
		}
		i2++ ;
		}
		return sb.toString();
	}
	
	public static String ChangeGB2NCR(String str) {
       String res = "";
       try {
        String[] w = new String[str.length()];
        for(int i =0;i<str.length();i++){
        w[i]= str.substring(i,i+1);
         byte[] utf_16E = w[i].getBytes("utf-16be");
                String Str16k = byteTo16String(utf_16E);
                int ncrNum = Integer.parseInt(Str16k, 16);
                res = res + "&#" +ncrNum + ";";        
        }
       } catch (UnsupportedEncodingException e) {
           e.printStackTrace();
       }
       return res;
   }

	private static String byteTo16String(byte[] bytes) {
		String strHex = "";
        StringBuilder sb = new StringBuilder("");
        for (int n = 0; n < bytes.length; n++) {
            strHex = Integer.toHexString(bytes[n] & 0xFF);
            sb.append((strHex.length() == 1) ? "0" + strHex : strHex); // 每个字节由两个字符表示，位数不够，高位补0
        }
        return sb.toString().trim();
	}
}
